package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.PolicyPermissionsAddDTO;
import org.jsola.hr.dto.PolicyPermissionsUpdateDTO;
import org.jsola.hr.query.PolicyPermissionsQuery;
import org.jsola.hr.service.IPolicyPermissionsService;
import org.jsola.hr.vo.PolicyPermissionsListVO;
import org.jsola.hr.vo.PolicyPermissionsVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 政策速递权限
 *
 * @author zhr
 */
@RestController("hrPolicyPermissionsController")
@RequestMapping(HrConstants.API_PATH + "/v1/policyPermissions")
public class PolicyPermissionsController implements IPolicyPermissionsControllerAPI {

    @Autowired
    private IPolicyPermissionsService policyPermissionsService;

    @Override
    @PostMapping
    public Result<PolicyPermissionsVO> save(@Valid @RequestBody PolicyPermissionsAddDTO policyPermissionsAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增政策速递权限权限校验

        return Result.success(
                policyPermissionsService.save(policyPermissionsAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PolicyPermissionsUpdateDTO policyPermissionsUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改政策速递权限权限校验

        int count = policyPermissionsService.update(policyPermissionsUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{policyPermissionsId}")
    public Result<Integer> delete(@PathVariable String policyPermissionsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除政策速递权限权限校验

        int count = policyPermissionsService.deleteByIds(tokenUser, policyPermissionsId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] policyPermissionsIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除政策速递权限权限校验

        int count = policyPermissionsService.deleteByIds(tokenUser, policyPermissionsIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{policyPermissionsId}")
    public Result<PolicyPermissionsVO> findById(@PathVariable String policyPermissionsId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政策速递权限权限校验

        return Result.success(
            policyPermissionsService.selectById(policyPermissionsId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PolicyPermissionsListVO>> listAll(@Valid PolicyPermissionsQuery policyPermissionsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政策速递权限权限校验

        return Result.success(
            policyPermissionsService.select(policyPermissionsQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<PolicyPermissionsListVO>> page(@Valid PolicyPermissionsQuery policyPermissionsQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            policyPermissionsService.selectPage(policyPermissionsQuery, tokenUser.getSiteId())
        );
    }


}

